﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FlowerShop.Models;

namespace FlowerShop.DataAccessLayer
{
    public class PaidTypesDAO
    {
        // Xử lí ở bảng Paidtype
        private static FlowerDBEntities db = new FlowerDBEntities();
        public static bool InsertPaidTypes(PaidTypeModels Paid)
        {
            PaidType pai = Mapper.paidtypeFromModels(Paid);
            try
            {
                db.AddToPaidTypes(pai);
                db.SaveChanges();
                return true;
            }
            catch (NullReferenceException)
            {
                return false;
            }
        }
        public static bool UpdatePaidTypes(PaidTypeModels Paid)
        {
            try
            {
                var pai = db.PaidTypes.Single(p => p.PaidTypeID == Paid.PaidTypeID);
                PaidType paty = Mapper.paidtypeFromModels(Paid);
                pai.PaidTypeID = paty.PaidTypeID;
                pai.PaidTypeName = paty.PaidTypeName;
                db.SaveChanges();
                return true;
            }
            catch (NullReferenceException)
            {
                return false;
            }
        }
        public static bool DeletePaidTypes(int id)
        {
            try
            {
                PaidType pai = db.PaidTypes.Single(p => p.PaidTypeID == id);
                db.PaidTypes.DeleteObject(pai);
                db.SaveChanges();
                return true;
            }
            catch (NullReferenceException)
            {
                return false;
            }
        }
        public static List<PaidType> SearchPaidTypes(string str)
        {
            List<PaidType> _PaidType = (from p in db.PaidTypes where p.PaidTypeName.ToLower().Contains(str.ToLower()) select p).ToList();
            return _PaidType;
        }
        public static PaidType SearchPaidTypes(int id)
        {
            var pai = db.PaidTypes.Single(p => p.PaidTypeID == id);
            return pai;
        }
        public static List<PaidType> GetAllPaidTypes()
        {
            List<PaidType> pai = (from p in db.PaidTypes select p).ToList();
            return pai;
        }
    }
}